package com.scuec.groupfive.service.impl;

import com.scuec.groupfive.entity.UserInfo;
import com.scuec.groupfive.mapper.UserClientMapper;
import com.scuec.groupfive.service.UserClientService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class UserClientServiceImpl implements UserClientService {
    @Autowired
    private UserClientMapper userClientMapper;

    @Override
    public List<UserInfo> getUserList() {
        return userClientMapper.getUserList();
    }

    @Override
    public UserInfo getUserById(Integer userId) {
        return userClientMapper.getUserById(userId);
    }

    @Override
    public int updateUserStatus(Integer userId, Integer status) {
        if (status != 0 && status != 1) {
            throw new IllegalArgumentException("状态值必须是0或1");
        }
        return userClientMapper.updateUserStatus(userId, status);
    }

    @Override
    public int updateUser(UserInfo userInfo) {
        return userClientMapper.updateUser(userInfo);
    }

    @Override
    public int addUser(UserInfo userInfo) {
        // 设置默认值
        if (userInfo.getStatus() == null) {
            userInfo.setStatus(1); // 默认启用
        }
        if (userInfo.getRole() == null) {
            userInfo.setRole("student"); // 默认角色
        }
        return userClientMapper.addUser(userInfo);
    }

    @Override
    public int deleteUser(Integer userId) {
        // 改为更新状态为0（禁用）而不是物理删除
        return userClientMapper.updateUserStatus(userId, 0);
    }
}